package com.wolfcode.contract.service.impl;

import java.util.List;

import com.wolfcode.common.utils.DateUtils;
import com.wolfcode.common.utils.SecurityUtils;
import com.wolfcode.contract.domain.vo.ContractVo;
import com.wolfcode.contract.enums.AffixSealStateEnum;
import com.wolfcode.contract.domain.vo.ContractVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wolfcode.contract.mapper.ContractMapper;
import com.wolfcode.contract.domain.Contract;
import com.wolfcode.contract.service.IContractService;
import org.springframework.util.Assert;

/**
 * 合同信息Service业务层处理
 * 
 * @author wolfcode
 * @date 2022-12-10
 */
@Service
public class ContractServiceImpl implements IContractService 
{
    public static final int AUDIT_PASS = 1;
    public static final int AUDIT_REJECT = -1;
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同信息
     * 
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectContractById(id);
    }

    /**
     * 查询合同信息列表
     * 
     * @param contract 合同信息
     * @return 合同信息
     */
    @Override
    public List<ContractVo> selectContractList(Contract contract)
    {
        return contractMapper.selectContractListInfo(contract);
    }

    /**
     * 新增合同信息
     * 
     * @param contract 合同信息
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        Assert.notNull(contract,"非法参数");
        Assert.notNull(contract.getAppendix(),"文件不能为空");
        contract.setAffixSealState(AffixSealStateEnum.NO.ordinal());
        contract.setAuditState(0);
        contract.setNullifyState(0);
        contract.setInputUser(SecurityUtils.getUserId());
        contract.setInputTime(DateUtils.getNowDate());
        return contractMapper.insertContract(contract);
    }

    /**
     * 修改合同信息
     * 
     * @param contractVo 合同信息
     * @return 结果
     */
    @Override
    public void updateContract(ContractVo contractVo)
    {
        Contract contractById = contractMapper.selectContractById(contractVo.getId());
        Assert.state(contractById.getAuditState()==0,"合同未审核状态才可以修改");
        Assert.state(contractById.getNullifyState() == 0,"合同作废状态不可以修改");
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractVo,contract);
        contractMapper.updateContract(contract);
    }

    /**
     * 批量删除合同信息
     * 
     * @param ids 需要删除的合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteContractByIds(ids);
    }

    /**
     * 合同作废
     * 
     * @param id 合同信息主键
     * @return 结果
     */
    @Override
    public void deleteContractById(Long id)
    {
        Assert.notNull(id,"非法参数");
        Contract contract = contractMapper.selectContractById(id);
        Assert.state(contract.getAffixSealState() == AffixSealStateEnum.NO.ordinal(),"未盖章状态才可以作废");
        contract.setNullifyState(-1);
        contractMapper.updateContract(contract);
    }

    /**
     * @param id 更新审核通过状态
     * @return
     */
    @Override
    public int updateAuditPass(Long id) {
        Assert.notNull(id,"非法参数！");
        Contract contract = new Contract();
        contract.setId(id);
        contract.setAuditState(AUDIT_PASS);
        return contractMapper.updateContract(contract);
    }

    /**
     * @param id 更新审核通过状态
     * @return
     */
    @Override
    public int updateAuditReject(Long id) {
        Assert.notNull(id,"非法参数!");
        Contract contract = new Contract();
        contract.setId(id);
        contract.setAuditState(AUDIT_REJECT);
        return contractMapper.updateContract(contract);
    }

    /**
     * @param id 更新盖章状态
     * @return
     */
    @Override
    public int stamp(Long id) {
        Assert.notNull(id,"非法参数");
        Contract contractById = contractMapper.selectContractById(id);
        Assert.notNull(contractById,"无该合同管理");
        Assert.state(contractById.getAuditState() == AUDIT_PASS,"审批通过才能盖章");
        Contract contract = new Contract();
        contract.setId(id);
        contract.setAffixSealState(AUDIT_PASS);
        return contractMapper.updateContract(contract);
    }
}
